package com.jeanbart.util;

import com.jeanbart.bean.Player;
import lombok.SneakyThrows;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class AgetB {

    public File getConfigFile() {
        File file = new File(System.getProperty("user.dir")).getParentFile();
        file = new File(file.toString() + "/config.json");
        return file;
    }

    public String getService(){
        File file = new File(System.getProperty("user.dir")).getParentFile();
        file=new File(file.toString()+"/config.json");
        String config = readToString(file);
        JSONObject jo = JSONObject.fromObject(config);
        return jo.getString("service");
    }
    public String getPath(){
        File file = new File(System.getProperty("user.dir")).getParentFile();
        file=new File(file.toString()+"/config.json");
        String config = readToString(file);
        JSONObject jo = JSONObject.fromObject(config);
        return jo.getString("path")+"/tempArenaInfo.json";
    }
    //读取整个文件内容
    @SneakyThrows
    public String readToString(File file) {
        Long filelength = file.length();
        byte[] filecontent = new byte[filelength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(filecontent);
    }

    public Player JSONObjectGetPlayer(JSONObject jsonObject){
        Player player = new Player();
        player.setAccountName(jsonObject.getString("nickname"));
        double rate=-1;
        if(jsonObject.getString("statistics")!="null") { //看有没有锁战绩
            player.setCount(""+jsonObject.getJSONObject("statistics").getJSONObject("pvp").getInt("battles"));
            player.setWinCount(""+jsonObject.getJSONObject("statistics").getJSONObject("pvp").getInt("wins"));
            if(jsonObject.getJSONObject("statistics").getJSONObject("pvp").getInt("battles")!=0){
                rate = 1.0 * jsonObject.getJSONObject("statistics").getJSONObject("pvp").getInt("wins")
                        / jsonObject.getJSONObject("statistics").getJSONObject("pvp").getInt("battles");
                player.setRate(rate);
            }else{
                player.setRate(0);
            }
        }else{
            player.setCount(""+-1);
            player.setWinCount(""+-1);
            rate=-1;
            player.setRate(rate);
        }
        return player;
    }


    public String NamelistGetString(List<String> names){
        if(names==null){
            return "";
        }
        StringBuilder s = new StringBuilder("");
        for(int i=0;i<names.size();i++){
            if(s.length()!=0){
                s.append(',');
            }
            s.append(names.get(i));
        }
        return s.toString();
    }
    public String HttpfileGetUids(String s){
        return "";
    }

    public String playerListGetNames(List<Player> playerList){
        StringBuilder names = new StringBuilder("");
        for(Player player:playerList){
            if(names.length()!=0){
                names.append(',');
            }
            names.append(player.getAccountName());
        }
        return names.toString();
    }

    public String playerListGetUids(List<Player> playerList){
        StringBuilder uids = new StringBuilder("");
        for(Player player:playerList){
            if(uids.length()!=0){
                uids.append(',');
            }
            uids.append(player.getUid());
        }
        return uids.toString();
    }
    public List<Player> JSONGetListPlayer(String json){
        List<Player> list = new ArrayList<Player>();
        JSONArray ja =  JSONObject.fromObject(json).getJSONArray("list");
        for(int i=0;i<ja.size();i++){
            if(ja.getJSONObject(i).getString("name").charAt(0)==':'){
                continue;
            }
            Player player = new Player();
            player.setAccountName(ja.getJSONObject(i).getString("name"));
            player.setShipId(ja.getJSONObject(i).getString("shipId"));
            player.setTeam(ja.getJSONObject(i).getInt("relation"));
            list.add(player);
        }
        return list;
    }

    public List<List<Player>> responseToList(JSONObject responseJSON){
        List<List<Player>> list =new ArrayList<List<Player>>();
        List<Player> Teammate = new ArrayList<Player>();
        List<Player> Enemy = new ArrayList<Player>();
        JSONArray ja1 = responseJSON.getJSONArray("teammate");
        JSONArray ja2 = responseJSON.getJSONArray("enemy");
        Player avg = new Player();
        int count = 0;
        double avgRate = 0;
        for (int i = 0;i<ja1.size();i++) {
            JSONObject jo = ja1.getJSONObject(i);
            Player player = new Player();
            player.setAccountName(jo.getString("accountName"));
            player.setCount(jo.getString("count"));
            player.setRate(Double.valueOf(jo.getString("rate")));
            player.setShipCount(jo.getString("shipCount"));
            player.setShipId(jo.getString("shipId"));
            player.setShipName(jo.getString("shipName"));
            player.setShipRate(Double.valueOf(jo.getString("shipRate")));
            player.setShipWinCount(jo.getString("shipWinCount"));
            player.setUid(jo.getString("uid"));
            player.setWinCount(jo.getString("winCount"));
            if(!player.getCount().equals("-1")){
                avgRate += player.getRate();
                count ++;
            }
            Teammate.add(player);
        }
        avg.setRate(avgRate/count);
        avg.setAccountName(":平均胜率");
        avg.setCount("--");
        avg.setShipName("--");
        avg.setShipCount("--");
        avg.setShipRate(-1);
        Teammate.add(avg);

        Player avg2 = new Player();
        avgRate = 0;
        count = 0;
        for (int i = 0;i<ja2.size();i++) {
            JSONObject jo = ja2.getJSONObject(i);
            Player player = new Player();
            player.setAccountName(jo.getString("accountName"));
            player.setCount(jo.getString("count"));
            player.setRate(Double.valueOf(jo.getString("rate")));
            player.setShipCount(jo.getString("shipCount"));
            player.setShipId(jo.getString("shipId"));
            player.setShipName(jo.getString("shipName"));
            player.setShipRate(Double.valueOf(jo.getString("shipRate")));
            player.setShipWinCount(jo.getString("shipWinCount"));
            player.setUid(jo.getString("uid"));
            player.setWinCount(jo.getString("winCount"));
            if(!player.getCount().equals("-1")){
                avgRate += player.getRate();
                count ++;
            }
            Enemy.add(player);
        }
        avg2.setRate(avgRate/count);
        avg2.setAccountName(":平均胜率");
        avg2.setCount("--");
        avg2.setShipName("--");
        avg2.setShipCount("--");
        avg2.setShipRate(-1);
        Enemy.add(avg2);
        list.add(Teammate);
        list.add(Enemy);
        return list;
    }
}
